package juc.callable;

import java.util.concurrent.*;

public class CallableDemo2 {

    // CompletionService用于提交一组Callable任务，其take方法返回已完成的一个Callable任务对应的Future对象。
    public static void main(String[] args) throws Exception {
        ExecutorService pool = Executors.newFixedThreadPool(10);
        CompletionService<Integer>   service = new ExecutorCompletionService<Integer>(pool);

        for(int i=0;i<10;i++) {
            final int num = i;
            service.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    return num;
                }
            });
        }

        for(int i=0;i<9;i++) {
            Future<Integer>  result = service.take();
            System.out.println(result.get());
        }
    }



}